package com.shop.trade.atom.atomsvc.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.common.model.InputBean;
import com.shop.common.model.ServiceBean;
import com.shop.common.model.param.trade.ShoppingCartInput;
import com.shop.common.model.result.trade.ShoppingCartOutput;
import com.shop.common.tmpl.BaseService;
import com.shop.trade.atom.atomsvc.IShopCartAtomSvc;
import com.shop.trade.atom.dao.ShoppingCartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShopCartAtom extends BaseService implements IShopCartAtomSvc {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    public ServiceBean<Integer> insertShoppingCart(ShoppingCartInput shoppingCartInput) {
        int i = shoppingCartMapper.insertSelective(shoppingCartInput);
        if (i > 0) {
            return resultData(shoppingCartInput.getId());
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<String> deleteShoppingCart(List<Integer> ids) {
        int i = shoppingCartMapper.deleteByPrimaryKeys(ids);
        if (opSuccess(i)) {
            return resultSuccess();
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<ShoppingCartOutput> selectShoppingCart(ShoppingCartInput shoppingCartInput) {
        List<ShoppingCartOutput> list = shoppingCartMapper.qryShopCartList(shoppingCartInput);
        if (list != null && list.size() > 0) {
            return resultData(list.get(0));
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<List<ShoppingCartOutput>> selectShoppingCartList(ShoppingCartInput shoppingCartInput) {
        List<ShoppingCartOutput> list = shoppingCartMapper.qryShopCartList(shoppingCartInput);
        if (list != null && list.size() > 0) {
            return resultData(list);
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<List<ShoppingCartOutput>> qryShoppingCarts4P(InputBean<ShoppingCartInput> inputBean) {
        PageHelper.startPage(inputBean.getIndex(), inputBean.getPageSize());
        List<ShoppingCartOutput> list = shoppingCartMapper.qryShopCartList(inputBean.getParam());
        PageInfo<ShoppingCartOutput> pageInfo = new PageInfo<>(list);
        ServiceBean serviceBean = new ServiceBean();
        if (list.isEmpty()) {
            serviceBean.setSuccess(false);
        } else {
            serviceBean.setSuccess(true);
            serviceBean.setData(pageInfo.getList());
            serviceBean.setMaxSize(pageInfo.getTotal());
        }
        return serviceBean;
    }

    @Override
    public ServiceBean<Integer> updateShoppingCart(ShoppingCartInput shoppingCartInput) {
        int i = shoppingCartMapper.updateByPrimaryKeySelective(shoppingCartInput);
        if (opSuccess(i)) {
            return resultSuccess();
        }
        return resultFailed();
    }
}
